<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            条码合批
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">扫描条码1</div>
                    <input type="text" class="aui-input" placeholder="请扫描条码" style="width: 80%;" id="tm" onclick="getIdSelector();">
                    <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem" onclick="toScanner(1);"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号1</div>
                    <input type="text" class="aui-input" disabled="true" id="mrlCode" style="background-color:#d0d0d0">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号1</div>
                    <input type="text" class="aui-input" disabled="true" id="lotCode" style="background-color:#d0d0d0">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">数量1</div>
                    <input type="text" class="aui-input" disabled="true" id="qty" style="background-color:#d0d0d0">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号1</div>
                    <input type="text" class="aui-input" disabled="true" id="boxnum" style="background-color:#d0d0d0">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">扫描条码2</div>
                    <input type="text" class="aui-input" placeholder="请扫描条码" style="width: 80%;" id="tm2" onclick="getIdSelector();">
                    <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem" onclick="toScanner(2);"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号2</div>
                    <input type="text" class="aui-input" disabled="true" id="mrlCode2" style="background-color:#d0d0d0">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号2</div>
                    <input type="text" class="aui-input" disabled="true" id="lotCode2" style="background-color:#d0d0d0">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">数量2</div>
                    <input type="text" class="aui-input" disabled="true" id="qty2" style="background-color:#d0d0d0">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号2</div>
                    <input type="text" class="aui-input" disabled="true" id="boxnum2" style="background-color:#d0d0d0">
                </div>
            </li>

            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">打印机</div>
                    <select id="printName">
                    </select>
                </div>
            </li> -->
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">蓝牙设备</div>
                    <select id="bluetooth">
                    </select>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="print()">合批打印</div>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="bluetoothConnect()">蓝牙连接</div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    var scanRule = ""; //条码解析规则

    //初始化查询仓库下拉框
    apiready = function() {

        $("#tm").focus();
        // //查询打印机下拉框
        // selectPrintName();

        //查询条码解析规则
        initScanRule();

        //获取蓝牙列表
        getBluetoothCombox();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        $("#" + el.id).select();
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //查询蓝牙设备
    function getBluetoothCombox() {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.searchBluetooth({}, function(ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    var list = ret.list;
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("bluetooth"), html);

                    //取缓存中蓝牙设备打印机
                    var bluetooth = localStorage.getItem('bluetooth');
                    if (bluetooth != null && bluetooth != undefined && bluetooth != "") {
                        $("#bluetooth").val(bluetooth);
                    }

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //蓝牙连接
    function bluetoothConnect() {
        var address = $("#bluetooth").val();
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙"
            });
            return;
        }
        // console.log(address);
        // api.showProgress({
        //     title: '连接中...',
        // });
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.connectBluetooth({
            address: address,
        }, function(ret, err) {
            if (ret) {
                // api.hideProgress();
                if (ret.code == "S") {
                    api.toast({
                        msg: ret.message
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }

    // //查询打印机下拉框
    // function selectPrintName() {
    //     var un = localStorage.getItem('un');
    //     var serverUrl = localStorage.getItem('serverUrl');
    //     var url = serverUrl + "/app/getServicePrinterList";
    //     var data = {
    //         values: { ////以表单方式提交参数（JSON对象）
    //             un: un
    //         }
    //     };
    //     api.ajax({
    //         url: url,
    //         method: 'post',
    //         dataType: 'json',
    //         timeout: 300,
    //         returnAll: false,
    //         data: data
    //     }, function(ret, err) {
    //         if (ret) {
    //             var data = JSON.stringify(ret);
    //             var myobj = eval("(" + data + ")");
    //             var list = eval(myobj.list);
    //             var html = "<option value=''>--请选择--</option>";
    //             for (var i = 0; i < list.length; i++) {
    //                 html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
    //             }
    //             $api.html($api.byId("printName"), html);
    //
    //             //取缓存中打印机
    //             var printName = localStorage.getItem('printName');
    //             if (printName != null && printName != undefined && printName != "") {
    //                 $("#printName").val(printName);
    //             }
    //         }
    //     });
    // }

    //打印
    // function print() {
    //     var tm = $("#tm").val();
    //     var boxnum = $("#boxnum").val();
    //     var qty = $("#qty").val();
    //     var supplierCode = $("#supplierCode").val();
    //     var mrlCode = $("#mrlCode").val();
    //     var lotCode = $("#lotCode").val();
    //     var kanban = $("#kanban").val();
    //     var purchaseCode = $("#purchaseCode").val();
    //     var prodDate = $("#prodDate").val();
    //     var customer = $("#customer").val();
    //     var tmID = $("#tmID").val();
    //     var newQty = $("#newQty").val();
    //     var printName = $("#printName").val();
    //
    //     //发货单必输校验
    //     if (tm == "" || tm == null || tm == undefined) {
    //         api.toast({
    //             msg: '请扫描条码！',
    //             duration: 3000
    //         });
    //         return;
    //     }
    //     if (newQty == null || newQty == "" || newQty == undefined || parseInt(newQty) <=
    //         0) {
    //         api.toast({
    //             msg: '拆分数量必须大于0！',
    //             duration: 3000
    //         });
    //         return;
    //     }
    //     if (qty == null || qty == "" || qty == undefined) {
    //         api.toast({
    //             msg: '原箱数量必须大于0！',
    //             duration: 3000
    //         });
    //         return;
    //     }
    //     if (parseInt(newQty) > parseInt(qty)) {
    //         api.toast({
    //             msg: '拆分数量必须小于原箱数量！',
    //             duration: 3000
    //         });
    //         return;
    //     }
    //
    //     //打印机非空校验
    //     if (printName == null || printName == "" || printName == undefined) {
    //         api.toast({
    //             msg: '请选择打印机！',
    //             duration: 3000
    //         });
    //         return;
    //     }
    //
    //     localStorage.setItem('printName', printName);
    //
    //     api.showProgress({
    //         title: '保存中...',
    //     });
    //     var un = localStorage.getItem('un');
    //     var serverUrl = localStorage.getItem('serverUrl');
    //     var url = serverUrl + "/app/tmSplit";
    //     var params = new Object();
    //     params.url = url;
    //     params.dataType = 'json';
    //     params.method = 'post';
    //     var data = {
    //         values: {
    //             tm: tm,
    //             newQty: newQty,
    //             printName: printName,
    //             un: un
    //         }
    //     };
    //     api.ajax({
    //         url: url,
    //         method: 'post',
    //         dataType: 'json',
    //         timeout: 300,
    //         returnAll: false,
    //         data: data
    //     }, function(ret, err) {
    //         if (ret) {
    //             var data = JSON.stringify(ret);
    //             var myobj = eval("(" + data + ")");
    //             if (myobj.code == "0") {
    //                 api.hideProgress();
    //                 api.toast({
    //                     msg: "打印成功",
    //                     duration: 3000
    //                 });
    //                 //清空表单数据
    //                 $("#tm").val("");
    //                 $("#boxnum").val("");
    //                 $("#qty").val("");
    //                 $("#supplierCode").val("");
    //                 $("#mrlCode").val("");
    //                 $("#lotCode").val("");
    //                 $("#kanban").val("");
    //                 $("#purchaseCode").val("");
    //                 $("#prodDate").val("");
    //                 $("#customer").val("");
    //                 $("#tmID").val("");
    //                 $("#newQty").val("");
    //                 //光标回到条码
    //                 $("#tm").focus();
    //
    //                 //失败
    //             } else if (myobj.code == "1") {
    //                 api.hideProgress();
    //                 api.toast({
    //                     msg: myobj.message,
    //                     duration: 3000
    //                 });
    //             }
    //         }
    //     });
    // }

    //打印
    function print() {
        var tm = $("#tm").val();
        var mrlCode = $("#mrlCode").val();
        var lotCode = $("#lotCode").val();
        var qty = $("#qty").val();
        var boxnum = $("#boxnum").val();

        var tm2 = $("#tm2").val();
        var mrlCode2 = $("#mrlCode2").val();
        var lotCode2 = $("#lotCode2").val();
        var qty2 = $("#qty2").val();
        var boxnum2 = $("#boxnum2").val();

        var address = $("#bluetooth").val();

        //条码
        if (tm == "" || tm == null || tm == undefined) {
            api.toast({
                msg: '请扫描条码1！',
                duration: 3000
            });
            return;
        }
        //物料号非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            api.toast({
                msg: '物料号1不能为空！',
                duration: 3000
            });
            return;
        }
        //批次号非空校验
        if (lotCode == "" || lotCode == null || lotCode == undefined) {
            api.toast({
                msg: '批次号1不能为空！',
                duration: 3000
            });
            return;
        }
        //批次号非空校验
        if (qty == "" || qty == null || qty == undefined || parseFloat(qty) <= 0) {
            api.toast({
                msg: '数量1必须大于0！',
                duration: 3000
            });
            return;
        }

        //条码2
        if (tm2 == "" || tm2 == null || tm2 == undefined) {
            api.toast({
                msg: '请扫描条码1！',
                duration: 3000
            });
            return;
        }
        //物料号2非空校验
        if (mrlCode2 == "" || mrlCode2 == null || mrlCode2 == undefined) {
            api.toast({
                msg: '物料号1不能为空！',
                duration: 3000
            });
            return;
        }
        //批次号2非空校验
        if (lotCode2 == "" || lotCode2 == null || lotCode2 == undefined) {
            api.toast({
                msg: '批次号1不能为空！',
                duration: 3000
            });
            return;
        }
        //批次号非空校验
        if (qty2 == "" || qty2 == null || qty2 == undefined || parseInt(qty2) <= 0) {
            api.toast({
                msg: '数量2必须大于0！',
                duration: 3000
            });
            return;
        }

        //物料号一致非空校验
        if (mrlCode != mrlCode2) {
            api.toast({
                msg: '两个条码解析的物料号必须一样！',
                duration: 3000
            });
            return;
        }

        //批次号一致非空校验
        if (lotCode != lotCode2) {
            api.toast({
                msg: '两个条码解析的批次号必须一样！',
                duration: 3000
            });
            return;
        }

        //不能扫描同一个条码
        if(boxnum==boxnum2){
          api.toast({
              msg: '不能扫描同一条码合批！',
              duration: 3000
          });
          return;
        }

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙！"
            });
            return;
        }

        localStorage.setItem('bluetooth', address);

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/tmHpPlatform";
        var data = {
            values: {
                tm: tm, //条码
                tm2: tm2, //条码2
                qty2: qty2, //条码2数量
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {

                    //返回json数据
                    var json = myobj.json;

                    //调用PDA打印
                    printPDA(json, address);

                } else {

                    $("#tm").val("");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {

                $("#tm").val("");
                alert(JSON.stringify(err));
            }
        });
    }

    //打印
    function printPDA(json, address) {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.printerPlatform({
            json: json, //打印数据JSON
            address: address //蓝牙地址
        }, function(ret, err) {
            if (ret) {

                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });

                    //清空表单数据
                    $("#tm").val("");
                    $("#mrlCode").val("");
                    $("#lotCode").val("");
                    $("#qty").val("");
                    $("#boxnum").val("");
                    $("#tm2").val("");
                    $("#mrlCode2").val("");
                    $("#lotCode2").val("");
                    $("#qty2").val("");
                    $("#boxnum2").val("");

                    //光标回到条码
                    $("#tm").focus();

                } else {

                    $("#tm").val("");
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                $("#tm").val("");
                alert(JSON.stringify(err));
            }
        });
    }

    //扫描条码
    function toScanner(flag) {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {
            tmSplit(ret, flag);
        });
    }

    //扫描之后条码解析
    function tmSplit(sm, flag) {
        if (flag == 1) {
            $("#tm").val(sm);
        } else {
            $("#tm2").val(sm);
        }
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm, flag);

        } else {

            //后台解析
            postAnalyze(sm, flag);
        }
    }

    //本地解析
    function localAnalyze(sm, flag) {
        var arr = sm.split(",");
        //条码1
        if (flag == 1) {
            $("#mrlCode").val(arr[1]);
            $("#lotCode").val(arr[3]);
            $("#qty").val(arr[2]);
            $("#boxnum").val(arr[9]);
            $("#tm").select();

            //条码2
        } else if (flag == 2) {
            $("#mrlCode2").val(arr[1]);
            $("#lotCode2").val(arr[3]);
            $("#qty2").val(arr[2]);
            $("#boxnum2").val(arr[9]);
            $("#tm2").select();
        }
    }

    //后台解析条码
    function postAnalyze(sm, flag) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    if (flag == 1) {
                        for (var key in map) {
                            $("#" + key).val(map[key]);
                        }

                        $("#tm").select();
                    } else if (flag == 2) {
                        $("#mrlCode2").val(map['mrlCode']);
                        $("#lotCode2").val(map['lotCode']);
                        $("#qty2").val(map['qty']);
                        $("#boxnum2").val(map['boxnum']);

                        $("#tm2").select();
                    }

                } else if (myobj.code == '1') {
                    if (flag == 1) {
                        $("#tm").val("");
                        $("#tm").select();
                    } else if (flag == 2) {
                        $("#tm2").val("");
                        $("#tm2").select();
                    }

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //条码1监听回车事件
    $("#tm").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            tmSplit(tm, 1);
        }
    });

    //条码2监听回车事件
    $("#tm2").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            tmSplit(tm, 2);
        }
    });

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
